package com.wl.user.login.biz.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.entity.Follow;
import com.wl.user.login.biz.IFollowBiz;
import com.wl.user.login.mapper.FollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FollowBizImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowBiz {
    @Autowired
    private FollowMapper followMapper;

    @Override
    public int insertFollow(Follow follow) {
        if (follow.getFollowerId() == null || follow.getFollowingId() == null) {
            throw new IllegalArgumentException("关注关系用户不能为空");
        }
        return followMapper.insertFollow(follow);
    }

    @Override
    public int insertOrUpdateFollow(Follow follow) {

        return baseMapper.insertOrUpdateFollow(follow);
    }

    @Override
    public int updateStatus(Integer followerId, Integer followingId, Integer status) throws Exception {
        try {
            return followMapper.updateStatus(followerId, followingId, status);
        } catch (Exception e) {
            throw new Exception("更新关注状态失败");
        }
    }

    @Override
    public boolean existsFollowRelation(Integer followerId, Integer followingId) {
        return followMapper.existsFollowRelation(followerId, followingId);
    }

    @Override
    public Map<String, Long> countFollowStats(Integer followerId, Integer followingId) {
        // 参数校验
        if (followerId == null || followingId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        if (followerId <= 0 || followingId <= 0) {
            throw new IllegalArgumentException("用户ID必须大于0");
        }

        // 调用Mapper查询数据
        return followMapper.countFollowStats(followerId, followingId);
    }

    @Override
    public Long getFollowingIds(Integer userId) {
        return lambdaQuery()
                .eq(Follow::getFollowerId, userId)
                .eq(Follow::getStatus, 1)
                .count();
    }

    @Override
    public Long getFollowerIds(Integer userId) {
        return lambdaQuery()
                .eq(Follow::getFollowerId, userId)
                .eq(Follow::getStatus, 1)
                .count();
    }
}
